package com.starmark.gateway.log.access.disruptor.handler;

import com.lmax.disruptor.WorkHandler;
import com.starmark.gateway.log.access.disruptor.event.LogDataEvent;
import com.starmark.gateway.log.access.service.IGatewayAccessLogService;

import java.util.concurrent.Executor;

/**
 * this is disruptor consumer.
 *
 * @author xiaoyu(Myth)
 */
public class LogDataHandler implements WorkHandler<LogDataEvent> {

    private Executor executor;

    private IGatewayAccessLogService sysAccessLogService;

    /**
     * Instantiates a new Log data handler.
     *
     * @param executor the executor
     */
    public LogDataHandler(final Executor executor, final IGatewayAccessLogService sysAccessLogService) {
        this.executor = executor;
        this.sysAccessLogService = sysAccessLogService;

    }

    @Override
    public void onEvent(final LogDataEvent event) {
        executor.execute(() -> {
            sysAccessLogService.save(event.getGatewayAccessLog());
            event.clear();
        });
    }
}
